import { injectReducer } from '../../store/reducers'
import TermsHome from './components/TermsHome'

export default (store) => ({
  path: 'terms',
  indexRoute: {
    component: TermsHome
  },
  childRoutes: [{
    path: 'manager/:termsId',
    getComponent(nextState, cb) {
      require.ensure([], (require) => {
        const QuestionsManager = require('./routes/containers/ManagerContainer').default
        const reducer = require('./routes/modules/questions').default
        injectReducer(store, { key: 'questions', reducer })
        cb(null, QuestionsManager)
      }, 'questions')
    }
  }],
  /*  Async getComponent is only invoked when route matches   */
  getComponent(nextState, cb) {
    /*  Webpack - use 'require.ensure' to create a split point
        and embed an async module loader (jsonp) when bundling   */
    require.ensure([], (require) => {
      /*  Webpack - use require callback to define
          dependencies for bundling   */
      const Terms = require('./containers/TermsContainer').default
      const reducer = require('./modules/terms').default

      /*  Add the reducer to the store on key 'counter'  */
      injectReducer(store, { key: 'terms', reducer })

      /*  Return getComponent   */
      cb(null, Terms)

      /* Webpack named bundle   */
    }, 'terms')
  }
})
